package com.amado.course47Properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 数据库连接
 */
public class DBConnection {
    private static String user = "student";
    private static String password = "student123";
    /**
     * 驱动
     */
    private static String className = "com.mysql.jdbc.Driver";
    //	private String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private static String url = "jdbc:mysql://47.98.129.170:3306/student?useUnicode=true&characterEncoding=utf8";


    public static Connection getConn() {
            try {
                Class.forName(className);
                Properties properties = new Properties();
//                properties.load(new FileInputStream("D:\\gitCode\\javase\\src\\main\\java\\com\\amado\\course47Properties\\jdbc.properties"));
                properties.load(DBConnection.class.getResourceAsStream("jdbc.properties"));
//                return DriverManager.getConnection(url, user, password);
			return DriverManager.getConnection(PropertiesUtil.getProperty("url"), properties);
		} catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

        return null;
    }
    // 测试数据库连接是否成功
    public static void main(String[] args) {
        System.out.println(getConn());
    }
}